home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / misc / emu / arosdev.lha / AROS / rom / utility / umult32.c < prev    next >
C/C++ Source or Header  |  1997-02-03  |  3KB  |  106 lines

  1. /*
  2.     Copyright (C) 1995-1997 AROS - The Amiga Replacement OS
  3.     $Id: umult32.c,v 1.7 1997/02/03 02:58:32 ldp Exp $
  4.  
  5.     Desc: Unsigned 32 bit multiplication function.
  6.     Lang: english
  7. */
  8. #include "utility_intern.h"
  9.  
  10. /*****************************************************************************
  11.  
  12.     NAME */
  13. #include <proto/utility.h>
  14.  
  15.         AROS_LH2(ULONG, UMult32,
  16.  
  17. /*  SYNOPSIS */
  18.         AROS_LHA(ULONG        , arg1, D0),
  19.         AROS_LHA(ULONG        , arg2, D1),
  20.  
  21. /*  LOCATION */
  22.         struct UtilityBase *, UtilityBase, 24, Utility)
  23.  
  24. /*  FUNCTION
  25.         Performs an unsigned 32-bit multiplication of arg1 * arg2 and
  26.         returns a 32 bit value.
  27.  
  28.     INPUTS
  29.         arg1, arg2  -   32 bit unsigned longs
  30.  
  31.     RESULT
  32.         arg1 * arg2
  33.  
  34.     NOTES
  35.         This can perform the multiplication either using the machines
  36.         native instructions (if they exist), or in software using a
  37.         simple algorithm (three multiplications, two shifts and
  38.         an addition.
  39.  
  40.         The utility.library math functions are unlike all other utility
  41.         functions in that they don't require the library base to be
  42.         loaded in register A6, and they also save the values of the
  43.         address registers A0/A1.
  44.  
  45.         This function is mainly to support assembly programers, and is
  46.         probably of limited use to higher-level language programmers.
  47.  
  48.     EXAMPLE
  49.  
  50.         LONG a = 352543;
  51.         LONG b = 52464;
  52.         LONG c = UMult32(a,b);
  53.         c == 1315946768
  54.  
  55.     BUGS
  56.  
  57.     SEE ALSO
  58.         utility/SMult32(), utility/UMult64(), utility/SMult64()
  59.  
  60.     INTERNALS
  61.         May be handled by code in config/$(KERNEL), may not be...
  62.  
  63.         It is for m68k-native...
  64.  
  65.         To emulate this operation we are performing the operation:
  66.  
  67.             (2^16 * a + b) * (2^16 * c + d)
  68.           = 2^32 * ab + 2^16 * ad + 2^16 * bc + bd
  69.           = 2^32 * ab + 2^16 ( ad + bc ) + bd
  70.  
  71.         Now since the result is a 32-bit number, the 2^32 term will have
  72.         no effect. (Since 2^32 > max (32-bit number).
  73.  
  74.         Therefore:
  75.         product = 2^16( ad + bc ) + bd
  76.  
  77.     HISTORY
  78.         29-10-95    digulla automatically created from
  79.                             utility_lib.fd and clib/utility_protos.h
  80.         18-08-96    iaint   Implemented as described above.
  81.  
  82. *****************************************************************************/
  83. {
  84.     AROS_LIBFUNC_INIT
  85.  
  86.     /* If we have native support for 32 * 32 -> 32, use that. */
  87.  
  88.     return arg1 * arg2;
  89.  
  90. #if 0
  91.     /* This is the equivalent to the emulating code,
  92.         see also config/m68k-native/sumult32.s
  93.     */
  94.     UWORD a0, a1, b0, b1;
  95.  
  96.     a1 = (arg1 >> 16) & 0xffff;
  97.     a0 = arg1 & 0xffff;
  98.     b1 = (arg2 >> 16) & 0xffff;
  99.     b0 = arg2 & 0xffff;
  100.  
  101.     return (((a0 * b1) + (a1 * b0)) << 16) + (b0 * a0);
  102. #endif
  103.  
  104.     AROS_LIBFUNC_EXIT
  105. } /* UMult32 */
  106.